﻿namespace YidanSoft.Library.EditorUtility.General
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using YidanSoft.Library.EditorUtility.Win32;

    public class TextUtil
    {
        private TextUtil()
        {
        }

        public static void DrawReverseString(Graphics g, string drawText, Font drawFont, Rectangle drawRect, Brush drawBrush, StringFormat drawFormat)
        {
            GraphicsContainer container = g.BeginContainer();
            g.TranslateTransform((float) ((drawRect.Left * 2) + drawRect.Width), (float) ((drawRect.Top * 2) + drawRect.Height));
            g.RotateTransform(180f);
            g.DrawString(drawText, drawFont, drawBrush, drawRect, drawFormat);
            g.EndContainer(container);
        }

        public static void DrawText(Graphics graphics, string text, Font font, Rectangle rect)
        {
            IntPtr hdc = graphics.GetHdc();
            IntPtr hObject = font.ToHfont();
            IntPtr ptr3 = WindowsAPI.SelectObject(hdc, hObject);
            WindowsAPI.SetBkMode(hdc, YidanSoft.Library.EditorUtility.Win32.BackgroundMode.TRANSPARENT);
            YidanSoft.Library.EditorUtility.Win32.RECT lpRect = new YidanSoft.Library.EditorUtility.Win32.RECT();
            lpRect.left = rect.Left;
            lpRect.top = rect.Top;
            lpRect.right = lpRect.left + rect.Width;
            lpRect.bottom = lpRect.top + rect.Height;
            WindowsAPI.DrawText(hdc, text, text.Length, ref lpRect, 0x50020);
            WindowsAPI.SelectObject(hdc, ptr3);
            WindowsAPI.DeleteObject(hObject);
            graphics.ReleaseHdc(hdc);
        }

        public static Size GetTextSize(Graphics graphics, string text, Font font)
        {
            IntPtr zero = IntPtr.Zero;
            if (graphics != null)
            {
                zero = graphics.GetHdc();
            }
            else
            {
                zero = WindowsAPI.GetDC(IntPtr.Zero);
            }
            IntPtr hObject = font.ToHfont();
            IntPtr ptr3 = WindowsAPI.SelectObject(zero, hObject);
            YidanSoft.Library.EditorUtility.Win32.RECT lpRect = new YidanSoft.Library.EditorUtility.Win32.RECT();
            lpRect.left = 0;
            lpRect.right = 0;
            lpRect.top = 0;
            lpRect.bottom = 0;
            WindowsAPI.DrawText(zero, text, text.Length, ref lpRect, 0x420);
            WindowsAPI.SelectObject(zero, ptr3);
            WindowsAPI.DeleteObject(hObject);
            if (graphics != null)
            {
                graphics.ReleaseHdc(zero);
            }
            else
            {
                WindowsAPI.ReleaseDC(IntPtr.Zero, zero);
            }
            return new Size(lpRect.right - lpRect.left, lpRect.bottom - lpRect.top);
        }

        public static Size GetTextSize(Graphics graphics, string text, Font font, ref Rectangle rc, DrawTextFormatFlags drawFlags)
        {
            IntPtr zero = IntPtr.Zero;
            if (graphics != null)
            {
                zero = graphics.GetHdc();
            }
            else
            {
                zero = WindowsAPI.GetDC(IntPtr.Zero);
            }
            IntPtr hObject = font.ToHfont();
            IntPtr ptr3 = WindowsAPI.SelectObject(zero, hObject);
            YidanSoft.Library.EditorUtility.Win32.RECT lpRect = new YidanSoft.Library.EditorUtility.Win32.RECT();
            lpRect.left = rc.Left;
            lpRect.right = rc.Right;
            lpRect.top = rc.Top;
            lpRect.bottom = rc.Bottom;
            WindowsAPI.DrawText(zero, text, text.Length, ref lpRect, (int) drawFlags);
            WindowsAPI.SelectObject(zero, ptr3);
            WindowsAPI.DeleteObject(hObject);
            if (graphics != null)
            {
                graphics.ReleaseHdc(zero);
            }
            else
            {
                WindowsAPI.ReleaseDC(IntPtr.Zero, zero);
            }
            return new Size(lpRect.right - lpRect.left, lpRect.bottom - lpRect.top);
        }
    }
}

